<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Communication Protocols
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora187.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora189.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Communication Protocols</H2>
<A NAME="@concepts360"></A>
The various client-server communications described in the previous section consisted 
of sending a string of characters ending in a carriage-return and receiving another.
However simple, this communication pattern defines a protocol.
If we wish to communicate more complex values, such as floats, matrices of 
floats, a tree of arithmetic expressions, a closure, or an object, we introduce
the problem of encoding these values. Many solutions exist according to 
the nature of the communicating programs, which can be characterized by 
the implementation language, the machine architecture, and in certain 
cases, the operating system.
Depending on the machine architecture, integers can be 
represented in many different ways (most significant bits on the left, on the 
right, use of tag bits, and size of a machine word).
To communicate a value between different programs, it is necessary 
to have a common representation of values, referred to as the external
representation<A NAME="text48" HREF="book-ora192.html#note48"><SUP><FONT SIZE=2>2</FONT></SUP></A>.
More structured values, such as records, just as integers, must have an external
representation.
Nonetheless, there are problems when certain languages allow 
constructs, such as bit-fields in C, which do not exist in other
languages.
Passing functional objects or objects, which contain pieces of code, poses 
a new difficulty. Is the code byte-compatible between the sender and receiver, and does 
there exist a mechanism for dynamically loading the code? 
As a general rule, the problem is simplified by supposing that the 
code exists on both sides. It is not the code itself that is transmitted, 
but information that allows it to be retrieved. 
For an object, the instance variables are communicated along with the object's
type, which allows retrieval of the object's methods.
For a closure, the environment is sent along with the address of its code.
This implies that the two communicating programs are actually the same 
executable.<BR>
<BR>
A second difficulty arises from the complexity of linked exchanges 
and the necessity of synchronizing communications involving 
many programs.<BR>
<BR>
We first present text protocols, later discussing acknowledgements 
and time limits between requests and responses. We also mention the 
difficulty of 
communicating internal values, in particular as it relates to 
interoperability between programs written in different languages.<BR>
<BR>
<A NAME="toc283"></A>
<H3> Text Protocol</H3> Text protocols, that is, communication in ASCII format, 
are the most common because they are the simplest to implement and the 
most portable.
When a protocol becomes complicated, it may become difficult 
to implement. In this setting, we define a grammar to describe 
the communication format. This grammar may be rich, but it will 
be up to the communicating programs to handle the work of 
coding and interpreting the text strings sent and received.<BR>
<BR>
As a general rule, a network application does not allow viewing 
the different layers of protocols in use. This is typified 
by the case of the HTTP protocol, which allows a browser 
to communicate with a Web site.<BR>
<BR>

<H4> The HTTP Protocol</H4>
<A NAME="sec-pro-http"></A>
<A NAME="@concepts361"></A>
The term ``HTTP'' is seen frequently in advertising. 
It corresponds to the communication protocol used by Web applications.
The protocol is completely described on the page of the 
W3 Consortium:


<H3> Link </H3> <HR>

<A HREF="http://www.w3.org">http://www.w3.org</A>


<HR>


This protocol is used to send requests from browsers (Communicator,
Internet Explorer, Opera, etc.) and to return the contents of 
requested pages. A request made by a browser contains the name 
of the protocol (HTTP), the name of the machine 
(www.ufr-info-p6.jussieu.fr), 
and the path of the requested page (/Public/Localisation/index.html). 
Together these components define a URL (Uniform Resource
Locator):
<PRE>
http://www.ufr-info-p6.jussieu.fr/Public/Localisation/index.html
</PRE>When such a URL is requested by a browser, a connection over a socket 
is established between the browser and the server running on the 
indicated server, by default on port 80. Then the browser sends 
a request in the HTTP format, like the following:
<PRE>
GET /index.html HTTP/1.0
</PRE>The server responds in the protocol HTTP, with a header: 
<PRE>
HTTP/1.1 200 OK
Date: Wed, 14 Jul 1999 22:07:48 GMT
Server: Apache/1.3.4 (Unix) PHP/3.0.6 AuthMySQL/2.20
Last-Modified: Thu, 10 Jun 1999 12:53:46 GMT
 
Accept-Ranges: bytes
Content-Length: 3663
Connection: close
Content-Type: text/html
</PRE>This header indicates that the request has been accepted (code 200 OK), the kind of server,
the modification date for the page, the length of the send page and the type of content which
follows.
Using the GET commmand in the protocol (HTTP/1.0), only the HTML page is transferred. 
The following connection with telnet allows us to see what is actually transmitted:
<PRE>
$ telnet www.ufr-info-p6.jussieu.fr 80
Trying 132.227.68.44...
Connected to triton.ufr-info-p6.jussieu.fr.
Escape character is '^]'.
GET


&lt;!-- index.html --&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Serveur de l'UFR d'Informatique de Pierre et Marie Curie&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;

&lt;IMG SRC="/Icons/upmc.gif" ALT="logo-P6" ALIGN=LEFT HSPACE=30&gt;
Unit&amp;eacute; de Formation et de Recherche 922 - Informatique&lt;BR&gt;
Universit&amp;eacute; Pierre et Marie Curie&lt;BR&gt;
4, place Jussieu&lt;BR&gt;
75252 PARIS Cedex 05, France&lt;BR&gt;&lt;P&gt; 
....
&lt;/BODY&gt;
&lt;/HTML&gt;
&lt;!-- index.html --&gt;

Connection closed by foreign host.
</PRE>The connection closes once the page has been copied.
The base protocol is in text mode so that the language may 
be interpreted. Note that images are not transmitted with the 
page. It is up to the browser, when analyzing the syntax of the 
HTML page, to observe anchors and images (see the <TT>IMG</TT> tags
in the transmitted page). At this time, the browser sends a new 
request for each image encountered in the HTML source; there
is a new connection for each image. The images are displayed when 
they are received. For this reason, images are often 
displayed in parallel.<BR>
<BR>
The HTTP protocol is simple enough, but it transports information
in the HTML language, which is more complex.<BR>
<BR>
<A NAME="toc284"></A>
<H3> Protocols with Acknowledgement and Time Limits</H3>
When a protocol is complex, it is useful that the receiver 
of a message indicate to the sender that it has received the
message and that it is grammatically correct.
The client blocks while waiting for a response before
working on its tasks. If the part of the server handling this 
request has a difficulty interpreting the message, the server
must indicate this fact to the client rather than ignoring the 
request. The HTTP protocol has a system of error codes.
A correct request results in the code 200. A badly-formed request
or a request for an unauthorized page results in an error code 4xx or 5xx 
according to the nature of the error. These error codes allow 
the client to know what to do and allow the server to record the 
details of such incidents in its log files.<BR>
<BR>
<A NAME="@fonctions490"></A>
<A NAME="@fonctions491"></A>
<BR>
<BR>
When the server is in an inconsistent state, it can always 
accept a connection from a client, but risks never sending 
it a response over the socket. For avoiding these blocking 
waits, it is useful to fix a limit to the time for transmission
of the response. After this time has elapsed, the client 
supposes that the server is no longer responding. 
Then the client can close this connection in order to go 
on to its other work. This is 
how WWW browsers work. When a request has no response 
after a certain time, the browser decides to indicate that 
to the user. Objective CAML has input-output with time limits. In 
the <TT>Thread</TT> library, the functions <TT>wait_time_read</TT> and
<TT>wait_time_write</TT> suspend execution until a 
character can be read or written, within a certain time limit.
As input, these function take a file descriptor and a time limit 
in seconds:
<I>Unix.file_descr -&gt; float -&gt; bool</I>. 
If the time limit has passed, the function returns 
<TT>false</TT>, otherwise the I/O is processed.<BR>
<BR>
<A NAME="toc285"></A>
<H3> Transmitting Values in their Internal Representation</H3>The interest in transmission of internal values comes from 
simplifying the protocol. There is no longer any need to 
encode and decode data in a textual format. The inherent 
difficulty in sending and receiving values in their 
internal representation are the same as those encountered 
for persistent values (see the <TT>Marshal</TT> library, 
page <A HREF="book-ora076.html#sec-persist">??</A>).
In effect, reading or writing a value in a file is 
equivalent to receiving the same value over a socket.<BR>
<BR>

<H4> Functional Values</H4>
In the case of transmitting a closure between two Objective CAML programs,
the code in the closure is not sent, only its environment and 
its code pointer (see figure&nbsp;<A HREF="book-ora115.html#fig-com4-C">12.9</A> page <A HREF="book-ora115.html#fig-com4-C">??</A>).
For this strategy to work, it is necessary that the server 
possess the same code in the same memory location. This implies
that the same program is running on the server as on the client.
Nothing, however, prevents the two programs from running different 
parts of the code at the same time. We adapt the matrix calculation service 
by sending a closure with an environment containing the data for 
calculation. When it is received, the server applies this closure
to () and the calculation begins.<BR>
<BR>
<A NAME="toc286"></A>
<H3> Interoperating with Different Languages</H3>
The interest in text protocols is that they are independent 
of implementation languages for clients and servers. In effect, 
the ASCII code is always known by programming languages.
Therefore, it is up to the client and to the server to analyze 
syntactically the strings of characters transmitted. 
An example of such an open protocol is the simulation of 
soccer players called RoboCup. <BR>
<BR>

<H4> Soccer Robots</H4>A soccer team plays against another team. Each member of the team 
is a client of a referee server. The players on the same team cannot communicate
directly with each other. They must send information through the server, which 
retransmits the dialog. The server shows a part of the field, 
according to the player's position.
All these communications follow a text protocol. A Web page that describes the protocol, 
the server, and certain clients:


<H3> Link </H3> <HR>

<A HREF="http://www.robocup.org/">http://www.robocup.org/</A>


<HR>


The server is written in C. The clients are written in different languages:
C, C++, SmallTalk, Objective CAML, etc.
Nothing prevents a team from fielding players written in different languages.<BR>
<BR>
This protocol responds to the interoperability needs between programs 
in different implementation languages. It is relatively simple, but 
it requires a particular syntax analyzer for each family of languages.<BR>
<BR>
<BR>
<BR>

<BR>
<BR>
<HR>
<A HREF="book-ora187.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora189.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
